package cn.wwk.notification.user.service.impl;

import cn.wwk.notification.common.domain.User;
import cn.wwk.notification.common.dto.LoginFormDTO;
import cn.wwk.notification.common.result.CodeMsg;
import cn.wwk.notification.common.result.Result;
import cn.wwk.notification.user.mapper.UserMapper;
import cn.wwk.notification.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wwk
* @description 针对表【user】的数据库操作Service实现
* @createDate 2022-05-13 16:16:01
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {


    @Resource
    private UserMapper mapper;

    /**
     * 查询数据库，校验用户
     * */
    @Override
    public Result<User> login(LoginFormDTO loginFormDTO) {
        //1.判断传入的参数是否为空
        if(loginFormDTO==null){
            //不存在
            return Result.error(CodeMsg.LOGIN_ERROR);
        }
        //2.获取登录信息对数据库进行验证
        String phone = loginFormDTO.getPhone();
        String password = loginFormDTO.getPassword();
        //3.封装用户信息
        // 根据map集合中的条件查询用户信息
        // SELECT * FROM user WHERE phone = ? AND password = ?
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("password", password);
        //4.mybatis_plus进行查询
        List<User> users = mapper.selectByMap(map);
        if(users==null || users.size()==0){
            return Result.error(CodeMsg.NO_USER);
        }
        //5.返回用户
        return Result.success(users.get(0));
    }

    /**
     * 获得某个状态下的所有用户列表
     * */
    @Override
    public Result<List<User>> getUserByStatus(int status) {
        HashMap<String, Object> map  = new HashMap<>();
        map.put("status", status);
        List<User> users = mapper.selectByMap(map);
        return Result.success(users);
    }
}




